﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Xml.Linq;
using System.ComponentModel;
using System.Web.Script.Services;
using Silverlight.DataSetConnector;
using Jumpton.SFA.Server.BusinessRule;
using Jumpton.SFA.Server.BusinessRule.Objective;
using Jumpton.SFA.Server.BusinessRule.CallRoute;
using Jumpton.SFA.Server.Entity;
using Jumpton.Data;
using Jumpton.Persistent;
using System.ServiceModel;


    /// <summary>
    ///MyService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。 
    [System.Web.Script.Services.ScriptService]

    public class RouteService : System.Web.Services.WebService
    {

        // MyStateManager myStateManager;
        StateManager myStateManager;

        public RouteService()
        {
            myStateManager = new StateManager();

        }

        /// <summary>
        /// 获取路线
        /// </summary>
        /// <param name="routeId"></param>
        /// <returns></returns>
        [WebMethod]
        [XmlSerializerFormat]
        public List<string> GetRoute(decimal routeId)
        {
            string LogContent = "";
            try
            {
                ROUTES route = new RouteManager().Load(routeId);
                List<string> list = new List<string>();

                list.Add(route.ID.ToString());
                list.Add(route.DESCRIPTION);
                list.Add(route.ROUTENUMBER.ToString());
                list.Add(route.ROUTEFORCE.ToString());
                list.Add(route.Active.ToString());
                LogContent += "获取客户类型:";
                if (list != null)
                    LogContent += "获取路线成功。";
                Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
            Jumpton.SFA.Server.BusinessRule.System.LogType.USER_DOWNLOAD, myStateManager.Person_Name,
            myStateManager.UserID, LogContent);
                return list;
            }
            catch (Exception ex)
            {
                Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
          Jumpton.SFA.Server.BusinessRule.System.LogType.GENERAL_EXCEPTION, myStateManager.Person_Name,
         myStateManager.UserID, "获取路线 - 异常:" + ex.Message);
                throw ex;
            }
        }

        /// <summary>
        /// 保存路线
        /// </summary>
        /// <param name="rid"></param>
        /// <param name="name"></param>
        /// <param name="no"></param>
        /// <param name="sort"></param>
        /// <param name="userid"></param>
        /// <param name="active"></param>
        /// <returns></returns>
        [WebMethod]
        [XmlSerializerFormat]
        public bool SaveRoute(decimal rid,string name,string no,string sort,decimal userid,bool active)
        {
            string LogContent = "";
            RouteManager rm = new RouteManager();
            ROUTES r=null;
            try
            {
                if (rid != 0)
                {
                    r = rm.Load(rid);
                }

                if (r == null)
                    r = new ROUTES();

                r.USERID = userid;
                r.DESCRIPTION = name;
                r.LAST_UPDATE_TIME = BusinessLogic.GetDateTimeDecimal(DateTime.Now);
                r.LAST_UPDATER = myStateManager.UserID.ToString();
                r.ROUTEFORCE = sort;
                r.ROUTENUMBER = decimal.Parse(no);
                r.Active = active;
               

                LogContent += "保存路线:";
                if ( rm.SaveRoutes(r))
                    LogContent += "保存路线成功。";
                Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
            Jumpton.SFA.Server.BusinessRule.System.LogType.Info_Change, myStateManager.Person_Name,
            myStateManager.UserID, LogContent);
            }
            catch(Exception ex)
            {
                Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
         Jumpton.SFA.Server.BusinessRule.System.LogType.GENERAL_EXCEPTION, myStateManager.Person_Name,
        myStateManager.UserID, "保存路线 - 异常:" + ex.Message);
                return false;
            }
            return true;
        }
        private string ConvertData(string p)
        {
            return p.Replace("|", "&brvbar");
        }
       
    }


